﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Windows.Media.Media3D10
{
    /// <summary>
    /// Matériau de rendu d'un sprite
    /// </summary>
    [System.Windows.Markup.ContentProperty("Brush")]
    public sealed class SpriteMaterial : Material
    {
        #region Dependency Properties
        /// <summary>
        /// Identifie la propriété de dépendance <see cref="Brush"/>.
        /// </summary>
        public static readonly DependencyProperty BrushProperty =
            DependencyProperty.Register("Brush", typeof(Brush), typeof(SpriteMaterial),
            new PropertyMetadata(new PropertyChangedCallback(Brush_PropertyChanged)));
        #endregion

        #region Dependency Properties Callbacks
        private static void Brush_PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (object.ReferenceEquals(sender, null))
                throw new ArgumentNullException("sender");
            if (object.ReferenceEquals(e, null))
                throw new ArgumentNullException("e");
            (sender as SpriteMaterial).OnBrushChanged(e);
        }
        #endregion

        #region Data
        private const string sprite_texture = "sprite_texture";
        #endregion

        #region Public Properties
        /// <summary>
        /// Texture du sprite
        /// </summary>
        public Brush Brush
        {
            get { return (Brush)GetValue(BrushProperty); }
            set { SetValue(BrushProperty, value); }
        }
        #endregion

        #region Private Members
        private void OnBrushChanged(DependencyPropertyChangedEventArgs e)
        {
            if (object.ReferenceEquals(e, null))
                throw new ArgumentNullException("e");
            this.Effect.GetVariableByName(sprite_texture).AsResource().SetTexture(Brush);
        }
        #endregion

        #region Freezable Implementation
        /// <summary>
        /// Une fois implémentée dans une classe dérivée, initialise une nouvelle instance de la classe dérivée <see cref="Freezable"/>. 
        /// </summary>
        /// <returns>Nouvelle instance.</returns>
        protected override Freezable CreateInstanceCore()
        {
            return new SpriteMaterial();
        }
        #endregion
    }
}
